<?php

	session_start();
	
	// check for login
	if(!isset($_SESSION['atbat']) && !isset($_COOKIE['remember_atbat'])) {
	
		// no login, move to index page
		header("Location: http://localhost/atbat/html/");
	
	}
	
	// connect to db
	require_once '../db/db.php';
	
	// get user id
	$sql = "SELECT user_id FROM users WHERE MD5(username) IN ('";
	$sql .= ((isset($_SESSION['atbat'])) ? $_SESSION['atbat'] : '') . "', '";
	$sql .= ((isset($_COOKIE['remember_atbat'])) ? $_COOKIE['remember_atbat'] : '') . "')";
		
	$id = $db->query($sql)->fetchAll();
	$user_id = $id[0]['user_id'];
	
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="http://localhost/atbat/html/" />
	<meta charset="utf-8">
	<title>AtBat :: Rosters</title>
	<meta name="description" content="">
	<meta name="author" content="Sean Mullin, http://parametercontraption.com">
	<!--  Mobile Viewport Fix j.mp/mobileviewport & davidbcalhoun.com/2010/viewport-metatag -->
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
	<link rel="shortcut icon" href="images/design/favicon.ico">
	<link rel="apple-touch-icon" href="images/design/apple-touch-icon.png">
	<link rel="stylesheet" href="css/screen.css">
	<!-- For the less-enabled mobile browsers like Opera Mini --><link rel="stylesheet" media="all" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css">
    <script src="js/modernizr-1.5.min.js"></script>
</head>
<body>
	<?php require_once 'include/header.html'; ?>
    <section id="content">
    	<h1>Rosters</h1>
        <article>
        	<h2>Create Rosters</h2>
            <p>Quickly and easliy create rosters to use in your games. You can also choose from current MLB team rosters updated daily.</p>
            <p>Create a <a href="create-roster">roster</a> now.</p>
        </article>
        <table cellspacing="0" summary="Shows the current rosters for this user">
        	<caption>Current Rosters</caption>
            <!-- TODO:
            	* show players associated with each roster via ajax
            -->
            <tr>
            	<th>Roster Name</th>
                <th>Team</th>
                <th>Number of Players</th>
                <th>Actions</th>
            </tr>
            <?php
				
				$sql = "SELECT rosters.roster_id, rosters.nickname, CONCAT(teams.city, ' ', teams.NAME) AS team FROM rosters LEFT JOIN teams ON teams.team_id = rosters.team_id WHERE rosters.user_id = " . $user_id;
				foreach($db->query($sql) as $row) { 
			
			?>
            <tr id="<?php echo $row[0]; ?>">
            	<td><a href="view-roster/<?php echo $row[0]; ?>"><?php echo $row[1]; ?></a></td>
                <td><?php echo $row[2]; ?></td>
                <td>
					<?php 
						// calc number of players in this roster
						$sql = "SELECT COUNT(player_id) AS num_players FROM rosters_players WHERE roster_id = " . $row[0];
						$number = $db->query($sql)->fetch(PDO::FETCH_ASSOC);
						echo $number['num_players'];	
					?>
                </td>
                <td><a href="edit-roster/<?php echo $row[0]; ?>">Edit</a> | <a href="delete-roster/<?php echo $row[0]; ?>" class="delete">Delete</a></td>
            </tr>
			<?php } ?>
        </table>
    </section>
    <?php require_once 'include/footer.html'; ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
    <script>
    	$(document).ready(function() {
			var r_id;
			$(".delete").click(function() {
				// show delete confirm dialog
				$("#dialog-confirm").dialog("open");
				r_id = $(this).attr("href");
				r_id = r_id.split("/");
				return false;					
			});
			
			$("#dialog-confirm").dialog({
				resizable: false,
				draggable: false,
				autoOpen: false,
				modal: true,
				buttons: {
					"Delete Roster": function() {
						// delete roster via ajax
						$.post(
							"delete-roster.php",
							{
								id: r_id[1],
								deleteRoster: true
							},
							function(data) {
								$(".error").remove();
								$(".success").remove();
								$(".notice").remove();
								$("#dialog-confirm").prepend(data);
								if($(data).hasClass('success')) {
									// close window after 2.5 seconds on success
									setTimeout(function() { $("#dialog-confirm").dialog("close"); }, 2500);
									// remove deleted roster from table
									$("#" + r_id[1]).fadeOut('slow');
								}
							}
						);
					},
					Cancel: function() {
						$(this).dialog("close");
					}
				}
			});
		});
    </script>
    <div id="dialog-confirm" title="Delete Roster">
    	<p>Do you really wish to delete this roster?</p>
        <p>Doing so will remove this roster and release each player from this roster. You will need to reasign a roster to each player.</p>
    </div>
</body>
</html>